package cn.drew.org.controller;

import cn.drew.basic.utils.AjaxResult;
import cn.drew.basic.utils.PageList;
import cn.drew.org.domain.Department;
import cn.drew.org.query.DepartmentQuery;
import cn.drew.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dept")
public class DeptController {

    @Autowired
    private DepartmentService departmentService;

    @GetMapping
    public List<Department> findALl(){
        List<Department> list = departmentService.findAll();
        return list;
    }

    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){
        Department department = departmentService.findById(id);
        return department;
    }

    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody Department department){
        if(department.getId() != null){
            departmentService.update(department);
        }else{
            departmentService.add(department);
        }

        return AjaxResult.success();
    }

    @DeleteMapping("/{id}")
    public AjaxResult Delete(@PathVariable("id") Long id){
        departmentService.delete(id);
        System.out.println(id);
        return AjaxResult.success();
    }

//    分页查询
    @PostMapping
    public PageList<Department> queryByPage(@RequestBody DepartmentQuery departmentQuery){
        PageList<Department> pageList = departmentService.queryByPage(departmentQuery);
        return pageList;
    }

    //批量删除
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        departmentService.patchDelete(ids);
        return AjaxResult.success();
    }
    
    //无线级数
    @GetMapping("/tree")
    public List<Department> tree(){
        List<Department> tree = departmentService.tree();
        System.out.println(tree);
        return departmentService.tree();

    }


    
}
